<template>
  <div class="header"></div>
  <div class="loginWraper">
    <div id="loginform" class="loginBox">
      <form class="form-horizontal" action="">
        <div class="account">
          <svg-icon
            name="account"
            width="26px"
            height="26px"
            class="svg-icon"
          ></svg-icon>
          <input
            :class="{ checkusername: isActiveUsername }"
            v-model="loginForm.username"
            id="text"
            name=""
            type="text"
            placeholder="账号"
            class="input size-L"
          />

          <p class="check-message">{{ errorMessage.username }}</p>
        </div>
        <div class="password">
          <svg-icon
            name="password"
            class="svg-icon"
            width="26px"
            height="26px"
          ></svg-icon>
          <input
            :class="{ checkpassword: isActivePassword }"
            v-model="loginForm.password"
            id=""
            name=""
            type="password"
            placeholder="密码"
            class="input size-L"
          />
          <p class="check-message">{{ errorMessage.password }}</p>
        </div>

        <div class="button-area">
          <div class="formControls">
            <!-- <input
              @click="login"
              name=""
              type="submit"
              class="btn btn-success radius size-btn"
              value="&nbsp;登&nbsp;&nbsp;&nbsp;&nbsp;录&nbsp;"
            />
            <input
              name=""
              type="reset"
              class="btn btn-default radius size-btn"
              value="&nbsp;取&nbsp;&nbsp;&nbsp;&nbsp;消&nbsp;"
            /> -->
            <el-button type="success" @click="submit()"
              >&nbsp;登&nbsp;&nbsp;&nbsp;&nbsp;录&nbsp;</el-button
            >
            <el-button type="info" plain @click="clearMessage()"
              >&nbsp;取&nbsp;&nbsp;&nbsp;&nbsp;消&nbsp;</el-button
            >
          </div>
        </div>
      </form>
    </div>
  </div>
  <div class="footer">Copyright 鱿鱼溪学院 by edu v1.1</div>
</template>

<script setup lang="ts">
import Schema from 'async-validator'

import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { errorMessage } from '@/api/user/type'
//引入用户相关的小仓库
import useUserStore1 from '@/store/modules/user1'
//引入layer弹窗插件
import { layer } from '@layui/layer-vue'
import getTime from '@/utils/time.ts'
// //控制输入框变红
const isActiveUsername = ref(false)
const isActivePassword = ref(false)

let $router = useRouter()
let useStore1 = useUserStore1()

//收集账号密码的数据
let loginForm = reactive({ username: '', password: '' })

//console.log(loginForm)

//console.log(Schema, 'Schema')

const rules = reactive({
  username: [
    {
      type: 'string',
      required: true,
      // validator(rule: any, value) {
      //   return value === 'muji'
      // },
      message: '输入错误'
    },
    {
      type: 'string',
      min: 3,
      max: 6,
      message: '用户名必须大于3位小于6位'
    }
  ],

  password: [
    { required: true, message: '请输入密码' },
    {
      validator(rule: any, value: any, callback: any) {
        value < 12 ? callback(new Error('不能少于12位')) : callback()
      }
    }
  ]
})

const validator = reactive(new Schema(rules))

const errorMessage: errorMessage = reactive({
  username: '',
  password: ''
})

const submit = () => {
  const result1 = validator
    .validate(loginForm, { firstFields: true }, (errors, fields) => {
      console.log('ol')

      if (errors) {
        // 校验不通过 do something
        //
        // console.log(fields)

        return
      }
      //  console.log('ok')

      // 校验通过 do something
    })
    .then((value) => {
      console.log(value, 'value')
    })
    .catch(({ errors, fields }) => {
      console.log(errors, 'errors')
      // console.log(fields, 'fields')
      // callback参数解析 共两个参数
      // errors 校验失败时返回一个所有 error 组成的数组,[{message: "输入错误", field: "name"}]，
      // 校验成功则errors值为null
      // fields 校验失败时返回一个对象,{name:[{message: "输入错误", field: "name"}]},
      // 校验成功则fileds值为null

      // for (let { field, message } of errors) {
      //   console.log(field, message)
      // }

      for (let { field, message } of errors) errorMessage[field] = message
    })
  // console.log(result1)
}

// //登录按钮的回调
// const submit = () => {
//   //清除错误信息
//   clearMessage()
//   validator
//     .validate(loginForm, {
//       firstFields: true
//     })
//     .then(
//       () => {
//         // 校验通过
//         console.log(' 校验通过,可以发起请求')
//         let result = useStore1.userLogin(loginForm)
//         console.log(result)
//         return result
//       },
//       ({ errors }) => {
//         // 校验未通过
//         // 显示错误信息
//         let field: keyof typeof errorMessage
//         let message: string

//         for ({ field, message } of errors) errorMessage[field] = message

//         //终止链条
//         return new Promise(() => {})
//       }
//     )
//     .then(
//       (data) => {
//         layer.msg(`${data} ${getTime()}好`, { icon: 1, time: 2000 })
//         $router.push('/')
//       },
//       (err: any) => {
//         layer.msg(err, { icon: 5, time: 2000 })
//       }
//     )
// }

// 清空校验错误提示
const clearMessage = () => {
  let key: keyof typeof errorMessage
  for (key in errorMessage) {
    errorMessage[key] = ''
  }
}
</script>

<style scoped lang="scss">
.header {
  top: 0;
  height: 60px;
  background: #426374 url(@/assets/images/logo.png) no-repeat 0 center;
}
.header,
.footer {
  position: absolute;
  left: 0;
  right: 0;
  width: 100%;
  z-index: 99;
}
.loginWraper {
  position: absolute;
  width: 100%;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  z-index: 1;
  background: #3283ac url(@/assets/images/admin-login-bg.jpg) no-repeat center;
}
.loginBox {
  position: absolute;
  width: 617px;
  height: 330px;
  background: url(@/assets/images/admin-loginform-bg.png) no-repeat;
  left: 50%;
  top: 50%;
  margin-left: -309px;
  margin-top: -184px;
  padding-top: 38px;
}
.loginBox .input {
  width: 360px;
  border: 1px solid #ddd;
  display: inline-block;
  -webkit-transition: all 0.2s linear 0s;
  -moz-transition: all 0.2s linear 0s;
  -o-transition: all 0.2s linear 0s;
  transition: all 0.2s linear 0s;
}
input[type='text']:focus {
  outline: none;
}
input[type='password']:focus {
  outline: none;
}

.form-horizontal {
  margin-left: 140px;
  margin-top: 30px;
}
.form-horizontal .form-label {
  margin-top: 3px;
  text-align: right;
  margin-right: 5px;
}
.password {
  margin-top: 20px;
}
.button-area {
  margin-left: 30px;
  margin-top: 30px;
}
.input.size-L {
  font-size: 16px;
  padding: 8px;
  height: 24px;
}
.size-btn {
  padding-left: 15px;
  margin-top: 40px;
  padding-right: 15px;
  padding-top: 8px;
  padding-bottom: 8px;
  margin-right: 10px;
}

.radius {
  border-radius: 4px;
}
//这里对图标组件进行了相对定位，进行了微调
.svg-icon {
  display: inline-block;
  margin-right: 5px;
  position: relative;
  top: 8px;
  right: 15px;
  cursor: pointer;
}
/* 按钮样式 */
.btn {
  display: inline-block;
  box-sizing: border-box;
  cursor: pointer;
  text-align: center;
  font-weight: 400;
  white-space: nowrap;
  vertical-align: middle;
  -moz-padding-start: npx;
  -moz-padding-end: npx;
  border: solid 1px #ddd;
  background-color: #fff;
  width: auto;
  *zoom: 1;
  *overflow: visible;
  -webkit-transition: background-color 0.1s linear;
  -moz-transition: background-color 0.1s linear;
  -o-transition: background-color 0.1s linear;
  transition: background-color 0.1s linear;
  font-size: 15px;
}

.btn-success {
  color: #fff;
  background-color: #5eb95e;
  border-color: #5eb95e;
}

.btn-default {
  background-color: #e6e6e6;
  border-color: #e6e6e6;
}

.footer {
  height: 46px;
  line-height: 46px;
  bottom: 0;
  text-align: center;
  color: #fff;
  font-size: 12px;
  background-color: #426374;
  font-style: italic;
}
.check-message {
  color: #d03050;
  position: relative;
  left: 30px;
  top: 8px;
}

.checkusername {
  border: 2px solid #d03050 !important ;
}
</style>
